<?php

/*
 * This file is part of the symfony package.
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

require_once(dirname(__FILE__).'/../lib/BasesfGuardUserActions.class.php');

/**
 * User management.
 *
 * @package    symfony
 * @subpackage plugin
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @version    SVN: $Id: actions.class.php 7633 2008-02-27 17:54:50Z fabien $
 */
class sfGuardUserActions extends BasesfGuardUserActions
{

    protected function updatesfGuardUserFromRequest()
  {
    $sf_guard_user = $this->getRequestParameter('sf_guard_user');

    if (isset($sf_guard_user['username']))
    {
      $this->sf_guard_user->setUsername($sf_guard_user['username']);
    }
    if (isset($sf_guard_user['password']))
    {
      $this->sf_guard_user->setPassword($sf_guard_user['password']);
    }
    if (isset($sf_guard_user['password_bis']))
    {
      $this->sf_guard_user->setPasswordBis($sf_guard_user['password_bis']);
    }
    if (isset($sf_guard_user['civilite']))
    {
      $this->sf_guard_user->setCivilite($sf_guard_user['civilite']);
    }
    if (isset($sf_guard_user['photo']))
    {
       $uploadDir = sfConfig::get('sf_upload_dir'). DIRECTORY_SEPARATOR . $fileRealName;

        $fileRealName1 = $this->getRequest()->getFileName('file1');
        $champ1 = 'file1';
        if (!empty($fileRealName1)){
            $this->sf_guard_user->setPhoto($sf_guard_user['photo']);
            $this->uploadPicture($this->sf_zebu_animal->getsfZebuPaysan().'_'.$fileRealName1, $champ1);
        }

      $this->sf_guard_user->setPhoto($sf_guard_user['photo']);
    }
    if (isset($sf_guard_user['nom']))
    {
      $this->sf_guard_user->setNom($sf_guard_user['nom']);
    }
    if (isset($sf_guard_user['prenom']))
    {
      $this->sf_guard_user->setPrenom($sf_guard_user['prenom']);
    }
    if (isset($sf_guard_user['complt_adresse']))
    {
      $this->sf_guard_user->setCompltAdresse($sf_guard_user['complt_adresse']);
    }
    if (isset($sf_guard_user['ville']))
    {
      $this->sf_guard_user->setVille($sf_guard_user['ville']);
    }
    if (isset($sf_guard_user['cp']))
    {
      $this->sf_guard_user->setCp($sf_guard_user['cp']);
    }
    if (isset($sf_guard_user['pays']))
    {
      $this->sf_guard_user->setPays($sf_guard_user['pays']);
    }
    if (isset($sf_guard_user['n_tel']))
    {
      $this->sf_guard_user->setNTel($sf_guard_user['n_tel']);
    }
    if (isset($sf_guard_user['n_tel2']))
    {
      $this->sf_guard_user->setNTel2($sf_guard_user['n_tel2']);
    }
    if (isset($sf_guard_user['email']))
    {
      $this->sf_guard_user->setEmail($sf_guard_user['email']);
    }
    if (isset($sf_guard_user['email_bis']))
    {
      $this->sf_guard_user->setEmailBis($sf_guard_user['email_bis']);
    }
    $this->sf_guard_user->setIsActive(isset($sf_guard_user['is_active']) ? $sf_guard_user['is_active'] : 0);
    if (isset($sf_guard_user['groups']))
    {
      $this->sf_guard_user->setGroups($sf_guard_user['groups']);
    }
    if (isset($sf_guard_user['permissions']))
    {
      $this->sf_guard_user->setPermissions($sf_guard_user['permissions']);
    }
  }

   protected function savesfGuardUser($sf_guard_user)
	{

		// saving user data into db
		parent::savesfGuardUser($sf_guard_user);

		// getting "foreign key" field name
		$fieldName = sfConfig::get('app_sf_guard_plugin_profile_field_name', 'user_id');

		// getting profile class
		$profileClass = sfConfig::get('app_sf_guard_plugin_profile_class', 'sfGuardUserProfile');
		$profilePeerClass =  $profileClass.'Peer';

		// building "setUserId" method from class and field retrieved above
		$method = 'set'.call_user_func_array(array($profilePeerClass, 'translateFieldName'), array($fieldName, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_PHPNAME));

		// recovering user profile
		$sf_guard_user_profile = $sf_guard_user->getProfile();

		// setting "foreign key". This operation must be done here. This is the only place where we can
		// be sure that the sfGuardUserId is set (since the user is saved/updated in the parent::savesfGuardUser).
		// A hidden field in the form wouldn't be effective when creating a new user, since its id is unknown.
		$sf_guard_user_profile->$method($sf_guard_user->getId());

		// saving user
		$sf_guard_user_profile->save();

	}

      protected function getLabels()
  {
    return array(
      'sf_guard_user{username}' => 'Login:',
      'sf_guard_user{password}' => 'Password:',
      'sf_guard_user{password_bis}' => 'Password (again):',
      'sf_guard_user{last_login}' => 'Last login:',
      'sf_guard_user{civilite}' => 'Civilit&eacute;:',
      'sf_guard_user{nom}' => 'Nom:',
      'sf_guard_user{prenom}' => 'Prénom:',
      'sf_guard_user{complt_adresse}' => 'Complément adresse:',
      'sf_guard_user{ville}' => 'ville;:',
      'sf_guard_user{cp}' => 'Code postal:',
      'sf_guard_user{pays}' => 'Pays:',
      'sf_guard_user{n_tel}' => 'Tel.Port:',
      'sf_guard_user{n_tel2}' => 'Tel.Fixe:',
      'sf_guard_user{email}' => 'Email:',
      'sf_guard_user{email_bis}' => 'Confirmer Email:',
      'sf_guard_user{is_active}' => 'Is active:',
      'sf_guard_user{groups}' => 'Groups/Roles:',
      'sf_guard_user{permissions}' => 'permissions:',
      'sf_guard_user{photo}' => 'Photo:',
    );
  }

     public function uploadPicture($fileRealName, $NomZoneTxt){

       $uploadDir = sfConfig::get('sf_upload_dir'). DIRECTORY_SEPARATOR . $fileRealName;

       $thumbnail = new sfThumbnail(85, 85);
       $thumbnail->loadFile($this->getRequest()->getFilePath($NomZoneTxt));
       $thumbnail->save(sfConfig::get('sf_upload_dir').'/thumbnail/'.$fileRealName, 'image/png');

       $this->getRequest()->moveFile($NomZoneTxt, $uploadDir);

   }

}
